package com.contract.spirit.ui.order.fragment

import android.os.Bundle
import com.contract.commonlib.base.BaseFragment
import com.contract.commonlib.http.ext.parseState
import com.contract.commonlib.refresh.IRefreshCallBack
import com.contract.commonlib.refresh.RefreshManager
import com.contract.commonlib.util.RecycleviewLayoutManger
import com.contract.commonlib.util.ext.showToast
import com.contract.spirit.R
import com.contract.spirit.config.Constant
import com.contract.spirit.databinding.FrgOrderBinding
import com.contract.spirit.manage.LiveDataEventManager
import com.contract.spirit.ui.order.OrderViewModel
import com.contract.spirit.ui.order.adapter.OrderAdapter
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * @author: zhengjun
 * created: 2021/9/27
 * desc:
 */
class OrderFragment : BaseFragment<FrgOrderBinding, OrderViewModel>() {
    lateinit var orderState: String
    lateinit var exchangeType: String
    var currentPage: Int = 1
    private val orderAdapter by lazy {
        OrderAdapter()
    }

    companion object {
        fun newsInstance(orderState: String?, exchangeType: String?): OrderFragment {
            val taskListFragment = OrderFragment()
            val bundle = Bundle()
            bundle.putString("orderState", orderState)
            bundle.putString(Constant.EXCHANGE_TYPE, exchangeType)
            taskListFragment.arguments = bundle
            return taskListFragment
        }
    }

    override fun setContentView(): Int {
        return R.layout.frg_order
    }

    override fun initData() {
        arguments?.apply {
            orderState = getString("orderState").toString()
            exchangeType = getString(Constant.EXCHANGE_TYPE).toString()
        }
        RecycleviewLayoutManger.setVerticalManager(mBinding.rvOrder, mContext)
        footView.setCustomBackGroudnColor(R.color.bgGrayColor)
        orderAdapter.setFooterView(footView)
        mBinding.rvOrder.adapter = orderAdapter
        orderAdapter.setOnItemChildClickListener { _, _, position ->
            val item = orderAdapter.getItem(position);
            gainPay(item.id)
        }
        getOrderList()
    }

    override fun onReload() {
        mViewModel.orderListRes.value = null
        getOrderList()
    }

    private fun gainPay(id: String) {
        mViewModel.gainPay(id)
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        onGetOrderObserve()
        onGainPayObserve()
    }

    private fun onGainPayObserve() {
        mViewModel.gainPayRes.observe(this, {
            parseState(it, {
                showToast("赔付成功")
                LiveEventBus.get<String>(LiveDataEventManager.GAIN_SUCCESS)
                    .post("gainSuccess")
            })
        })
    }

    private fun onGetOrderObserve() {
        mViewModel.orderListRes.observe(this, { it ->
            parseState(it, {
                if (currentPage == 1) {
                    orderAdapter.setList(it)
                } else {
                    orderAdapter.addData(it)
                }
                setDataLoadFinish(currentPage, it.size, mBinding.refreshLayout)
            }, {
                endLoad(currentPage, mBinding.refreshLayout)
            })
        })
    }

    override fun initRefresh() {
        RefreshManager.getInstance().initRefresh(mContext, footView, true, mBinding.refreshLayout,
            object : IRefreshCallBack {
                override fun onRefresh() {
                    currentPage = 1
                    getOrderList()
                }

                override fun onLoadMore() {
                    currentPage++
                    getOrderList()
                }
            })
    }

      fun getOrderList() {
        mViewModel.getOrderList(orderState, currentPage, exchangeType)
    }
}